package traverser;
/*    DynamicMatrix.java
 * Implementation of a Dynamic matrix of Strings for the SIGART project
 * 
 * Author: Zhongjun Jin, Daniel Rugeles
 * Date: 10/30/11
 */
import java.util.*;
public class DynamicMatrix{
    private String[][] matrix = new String[2][2];

    public void set(int x, int y, String value) {
        if (x >= matrix.length) {
            String[][] tmp = matrix;
            matrix = new String[x + 1][];
            System.arraycopy(tmp, 0, matrix, 0, tmp.length);
            for (int i = x; i < x + 1; i++) {
                matrix[i] = new String[y];
            }
        }

        if (y >= matrix[x].length) {
            String[] tmp = matrix[x];
            matrix[x] = new String[y + 1];
            System.arraycopy(tmp, 0, matrix[x], 0, tmp.length);
        }

        matrix[x][y] = value;
    }

    public String get(int x, int y) {
        return x >= matrix.length || y >= matrix[x].length ? null : matrix[x][y];
    }

    //get the size of the rows
    public int sizeRow(){
        return matrix.length;
    }
    
    //get the size of the column for the given row
    public int sizeColumn(int row){
        return matrix[row].length;
    }
    
    
    public static ArrayList<String> matchLink(DynamicMatrix to_visit_front,
            DynamicMatrix to_visit_back) {
        ArrayList<String> matches = new ArrayList<String>();
        /*System.out.println(to_visit_front.sizeColumn(0));
        System.out.println(to_visit_front.sizeColumn(1));
        System.out.println(to_visit_front.sizeColumn(2));
        System.out.println(to_visit_front.sizeRow());
    
        System.out.println(to_visit_back.sizeColumn(0));
        System.out.println(to_visit_back.sizeColumn(1));
        System.out.println(to_visit_back.sizeRow());*/
        
        

        for (int row2 = 0; row2 < to_visit_back.sizeRow(); row2++) {
        for (int column2 = 1; column2 < to_visit_back.sizeColumn(row2)-1; column2++) {
            for (int row = 0; row < to_visit_front.sizeRow(); row++) {
                for (int column = 1; column < to_visit_front.sizeColumn(row)-1; column++) {
                      //  System.out.println(to_visit_front.get(row,column));
                      //  System.out.println(to_visit_back.get(row2,column2));
                            if ((to_visit_front.get(row,column).equals(to_visit_back.get(row2, column2)))&&!(to_visit_front.get(row,column).equals("of")||to_visit_front.get(row,column).equals("and")||to_visit_front.get(row,column).equals("the")||to_visit_front.get(row,column).equals("at")||to_visit_front.get(row,column).equals("on")||to_visit_front.get(row,column).equals("and")||to_visit_front.get(row,column).equals("with")||to_visit_front.get(row,column).equals("disambiguation")))
                            matches.add(to_visit_front.get(row,0));
                    }
                }
            }
        }
        return matches;
    }
    
    
    public static void main(String[] args) {
    	String linkA="http://en.wikipedia.org/wiki/Space";
    	String linkB="http://en.wikipedia.org/wiki/Apple";
    	FreqFirstLastTraversal start=new FreqFirstLastTraversal(linkA);
    	FreqFirstLastTraversal end=new FreqFirstLastTraversal(linkB);
    	start.frequencyCount(); 
    	end.frequencyCount(); 
    	ArrayList<String> list =new ArrayList<String>();
    	list = DynamicMatrix.matchLink(start.returnDynamicMatrix(), end.returnDynamicMatrix());
    	
    	System.out.println("ME"+list);
    	
    	
    	
    	
    	
    	
    	/* DynamicMatrix matrix2d = new DynamicMatrix();

        
        //(column,row,messsage)
        matrix2d.set(0, 1, "asd");    
        matrix2d.set(10, 10, "asd");   
        matrix2d.set(90, 200, "sadf");
        matrix2d.set(90, 201, "sadfsssss");  
        matrix2d.set(90, 10, "sadffvg");  

        System.out.println(matrix2d.get(0, 1));     
        System.out.println(matrix2d.get(90, 201));   
        System.out.println(matrix2d.get(90, 10)); 
        System.out.println(matrix2d.sizeRow()); 
        System.out.println(matrix2d.sizeColumn(90)); 
        */
    }
}
